//
//  BTUtil.swift
//  TimeBoat
//
//  Created by Jun on 2022/9/7.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import Foundation

extension String {
    static func toHexStr(buf:[UInt8]?, prefix: String = "") -> String {
        var res = ""
        
        if (nil != buf) {
            for b in buf! {
                if res.count > 0 {
                    res.append(" ")
                }
                
                res = res.appending(prefix).appendingFormat("%02X", b)
            }
        }
        
        return res
    }
    
    func toInt8() -> Int8 {
        return Int8(self) ?? 0
    }
    
    func toUInt8() -> UInt8 {
        return UInt8(self) ?? 0
    }
    
    func toInt16() -> Int16 {
        return Int16(self) ?? 0
    }
    
    func toUInt16() -> UInt16 {
        return UInt16(self) ?? 0
    }
    
    func toInt32() -> Int32 {
        return Int32(self) ?? 0
    }
    
    func toUInt32() -> UInt32 {
        return UInt32(self) ?? 0
    }
}

extension UInt8 {
    func toHex() -> String {
        return String().appendingFormat("%02x", self)
    }
    
    static func toString(_ value: UInt8) -> String {
        return String(value)
    }
}

extension Int8 {
    func toHex() -> String {
        return String().appendingFormat("%02x", self)
    }
    
    static func toString(_ value: Int8) -> String {
        return String(value)
    }
}

extension UInt16 {
    func toHex() -> String {
        return String().appendingFormat("%04x", self)
    }
    
    static func toString(_ value: UInt16) -> String {
        return String(value)
    }
    
    static func toUInt16(buf: [UInt8], offset: Int) -> UInt16 {
        var value: UInt16 = 0
        
        if (offset + 2 <= buf.count) {
            value = UInt16(buf[offset] & 0xff) | (UInt16(buf[offset + 1] & 0xff) << 8)
        }
        
        return value
    }
}

extension Int16 {
    func toHex() -> String {
        return String().appendingFormat("%04x", self)
    }
    
    static func toString(_ value: Int16) -> String {
        return String(value)
    }
    
    static func toInt16(buf: [UInt8], offset: Int) -> Int16 {
        var value: Int16 = 0
        
        if (offset + 2 <= buf.count) {
            value = Int16(buf[offset] & 0xff) | (Int16(buf[offset + 1] & 0xff) << 8)
        }
        
        return value
    }
}

extension UInt32 {
    func toHex() -> String {
        return String().appendingFormat("%08x", self)
    }
    
    static func toString(_ value: UInt32) -> String {
        return String(value)
    }
    
    static func toUInt32(buf: [UInt8], offset: Int) -> UInt32 {
        var value: UInt32 = 0
        
        if (offset + 4 <= buf.count) {
            value = UInt32(buf[offset] & 0xff) | (UInt32(buf[offset + 1] & 0xff) << 8) | (UInt32(buf[offset + 2] & 0xff) << 16) | (UInt32(buf[offset + 3] & 0xff) << 24)
        }
        
        return value
    }
}

extension Int32 {
    func toHex() -> String {
        return String().appendingFormat("%08x", self)
    }
    
    static func toString(_ value: Int32) -> String {
        return String(value)
    }
    
    static func toInt32(buf: [UInt8], offset: Int) -> Int32 {
        var value: Int32 = 0
        
        if (offset + 4 <= buf.count) {
            value = Int32(buf[offset] & 0xff) | (Int32(buf[offset + 1] & 0xff) << 8) | (Int32(buf[offset + 2] & 0xff) << 16) | (Int32(buf[offset + 3] & 0xff) << 24)
        }
        
        return value
    }
}

extension Int {
    func toHex() -> String {
        return String().appendingFormat("%08x", self)
    }
    
    static func toString(_ value: Int) -> String {
        return String(value)
    }
}

extension Array {
    static func copyBuffer(buffer: [UInt8], offset: Int, length: Int) -> [UInt8] {
        var result = [UInt8]()
        
        if (offset + length <= buffer.count) {
            for i in offset...(offset + length - 1) {
                result.append(buffer[i])
            }
        }
        
        return result
    }
}
